﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain;
using Service.Interface;
using Dao.Interface;
using Dao;

namespace Service
{
    public class PageService : Service<Page>, IPageService
    {
        public IAreaRepository AreaRepository;

        /// <summary>
        /// 获取页分页集合
        /// </summary>
        /// <param name="areaID">分区ID</param>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="pageCount">总页数</param>
        /// <param name="sortType">排序类型</param>
        /// <returns></returns>
        public IList<Page> LoadAllByPage(int areaID, int pageIndex, int pageSize, out int pageCount, string sortType)
        {
            if (sortType == "Custom")//自定义排序
            {
                List<Page> pageList = new List<Page>(); //返回集合列表

                //获取总条数
                int totalCount = ((IPageRepository)(this.CurrentRepository)).LoadAll()
                            .Where(i => i.Area.ID == areaID && i.MarkDelete == false).Count();
                //获取总页数
                if (totalCount % pageSize == 0)
                {
                    pageCount = totalCount / pageSize;
                }
                else
                {
                    pageCount = totalCount / pageSize + 1;
                }
                //获取自定义排列分页数据
                Area area = this.AreaRepository.Get(areaID);
                string pageSort = area.PageSort;
                string[] sort = pageSort.Split('-');
                for (int i = (pageIndex - 1) * pageSize; i < (pageIndex - 1) * pageSize + pageSize; i++)
                {
                    if (i >= sort.Length)//如果一页不满PageSize的条数，则结束循环
                    {
                        break;
                    }
                    Page page = ((IPageRepository)(this.CurrentRepository)).Get(sort[i]);
                    pageList.Add(page);
                }

                return pageList;
            }
            else//按字段排序
            {
                return ((IPageRepository)(this.CurrentRepository)).LoadAllByPage(areaID, pageIndex, pageSize, out pageCount, sortType).ToList();
            }
        }

        /// <summary>
        /// 获取共享页的分页集合
        /// </summary>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="pageCount">总页数</param>
        /// <returns></returns>
        public IList<Page> LoadAllSharePage(int pageIndex, int pageSize, out int pageCount)
        {
            return ((IPageRepository)(this.CurrentRepository))
                .LoadAllSharePageByPage(pageIndex, pageSize, out pageCount)
                .ToList();
        }

        /// <summary>
        /// 获取共享页的分页集合
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="pageCount">总页数</param>
        /// <returns></returns>
        public IList<Page> LoadAllSharePage(int userID, int pageIndex, int pageSize, out int pageCount)
        {
            IList<Page> pageList = ((IPageRepository)(this.CurrentRepository))
                .LoadAllSharePageByPage(pageIndex, pageSize, out pageCount)
                .Where(u => u.ID == userID)
                .ToList();

            int totalCount = pageList.Count();
            if (totalCount % pageSize == 0)
            {
                pageCount = totalCount / pageSize;
            }
            else
            {
                pageCount = totalCount / pageSize + 1;
            }

            return pageList;
        }

        /// <summary>
        /// 更新页是否扔进垃圾箱
        /// </summary>
        /// <param name="pageID">页ID</param>
        /// <param name="delete">是否删除</param>
        public void UpdatePageMarkDelete(object pageID, bool delete)
        {
            if (pageID == null)
            {
                return;
            }
            Page page = ((IPageRepository)(this.CurrentRepository)).Get(pageID);
            page.MarkDelete = delete;
            ((IPageRepository)(this.CurrentRepository)).Update(page);
        }

        /// <summary>
        /// 共享笔记本
        /// </summary>
        /// <param name="pageID">页ID</param>
        /// <param name="share">是否共享</param>
        public void UpdatePageShare(object pageID, bool share)
        {
            if (pageID == null)
            {
                return;
            }
            Page page = ((IPageRepository)(this.CurrentRepository)).Get(pageID);
            page.Share = share;
            ((IPageRepository)(this.CurrentRepository)).Update(page);
        }

        /// <summary>
        /// 获取页集合
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <param name="delete">是否删除</param>
        /// <returns></returns>
        public IList<Page> LoadAllPage(object userID, bool delete)
        {
            if (userID == null)
            {
                return null;
            }
            return ((IPageRepository)(this.CurrentRepository)).LoadAllPage(userID, delete);
        }

        /// <summary>
        /// 保存页并增加分区中的页数
        /// </summary>
        /// <param name="page"></param>
        public void SaveAndAddPageCount(Page page)
        {
            if (page == null)
            {
                return;
            }
            ((IPageRepository)(this.CurrentRepository)).Save(page);
            Area area = this.AreaRepository.Get(page.Area.ID);
            area.PageCount = area.PageCount + 1;
            area.PageSort = page.ID + "-" + area.PageSort;
            this.AreaRepository.Update(area);
        }

        /// <summary>
        /// 删除页并减少分区中的页数
        /// </summary>
        /// <param name="page"></param>
        public void DeleteAndMinusPageCount(object pageID)
        {
            if (pageID == null)
            {
                return;
            }
            Page page = ((IPageRepository)(this.CurrentRepository)).Get(pageID);
            Area area = this.AreaRepository.Get(page.Area.ID);
            area.PageCount = area.PageCount - 1;
            this.AreaRepository.Update(area);
            ((IPageRepository)(this.CurrentRepository)).Delete(page);
        }

    }
}
